package cn.batch.daemon.hibernate.dao;

// Generated 2012-10-29 14:03:03 by Hibernate Tools 3.4.0.CR1

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.batch.daemon.hibernate.bean.PbocSummary;
import cn.batch.daemon.util.StringUtil;
/**
 * Home object for domain model class PbocSummary.
 * 
 * @see cn.batch.daemon.hibernate.bean.PbocSummary
 * @author Hibernate Tools
 */
public class PbocSummaryDao extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PbocSummaryDao.class);

	protected void initDao() {
		// do nothing
	}

	public void save(PbocSummary transientInstance) {
		log.debug("saving PbocSummary instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@SuppressWarnings("rawtypes")
	public Date queryBybatchRiskData(PbocSummary pboc) {
		DateFormat df = new  SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		Date queryDate = null;
	    String sql = "select max(querytime) from pboc_summary where  certno = "+"'"+pboc.getCertno()+"'";
	    if(StringUtil.isNotEmpty(pboc.getCerttype())){
	    	sql = sql+" and certtype = " +"'"+pboc.getCerttype()+"'";
	    }
	    if(StringUtil.isNotEmpty(pboc.getName())){
	    	sql = sql+" and name = " +"'"+pboc.getName()+"'";
	    }
		Session session = getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sql);
		List list = query.list();
		try {
			if (list.get(0) != null && !"".equals(list.get(0))) {
				queryDate = df.parse((String) list.get(0));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return queryDate;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<PbocSummary> queryByExample(PbocSummary transientInstance) {
		String sql = "select * from pboc_summary t where t.last_updated_ts = (select max(t1.last_updated_ts) from pboc_summary t1 " +
				"where t.credit_id = t1.credit_id and t1.name = '"+ transientInstance.getName() +"' and t1.certtype = '"+ transientInstance.getCerttype() +"' and t1.certno = '"+ transientInstance.getCertno() +"') and rownum = 1";
		Session session = getSessionFactory().getCurrentSession();
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(PbocSummary.class);
		List<PbocSummary> list = query.list();
		return list;
	}
	/*public List<PbocSummary> queryByExample(PbocSummary transientInstance) {
		try {
			List<PbocSummary> list = getHibernateTemplate().findByExample(transientInstance);
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}*/
	
	public void delete(PbocSummary transientInstance) {
		log.debug("delete PbocSummary instance");
		try {
			getHibernateTemplate().delete(transientInstance);	
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
					
	}
	public void deleteBatchRiskDataByBachNo(String date) {
		 String sql = " delete from BATCH_RISK_DATA  where BATCH_NO = "+"'"+date+"'";
		 Session session = getSessionFactory().getCurrentSession();
		 Query query = session.createSQLQuery(sql);
		 query.executeUpdate();
	}
}
